/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ch.bbcnet.awesomeplayer;

import com.phidgets.PhidgetException;
import com.phidgets.RFIDPhidget;
import com.phidgets.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextField;

/**
 *
 * @author efellf
 */
public class RFID{
    private static RFID rfid = null;
    private String Tag = null;
    private static JTextField ps;
    public RFID(final JTextField ps) throws PhidgetException{
        this.ps=ps;
        final RFIDPhidget rfid = new RFIDPhidget();
        // Das RFIDPhidget wird per USB angeschlossen
        rfid.addAttachListener(new AttachListener(){
        
            @Override
            public void attached(AttachEvent ae) {
                RFIDPhidget rfid = (RFIDPhidget) ae.getSource();
                try {
                    //System.out.println("RFID.java: RFID Connected, \tID:"+rfid.getDeviceID()+"\tNAME:"+rfid.getDeviceName());
                    
                    for(int i = 0;i<30;i+=3){
                        rfid.setLEDOn(true);
                        waitms((int)Math.pow(i, 1.2));
                        rfid.setLEDOn(false);
                        waitms((int)Math.pow(i, 1.2));
                        
                    }
                    for(int i = 0; i < 10;i++){
                        rfid.setLEDOn(true);
                        waitms((int)Math.pow(30, 1.2));
                        rfid.setLEDOn(false);
                        waitms((int)Math.pow(30, 1.2));
                    }
                    rfid.setLEDOn(false);

                    
                    rfid.setAntennaOn(true);
                } catch (PhidgetException ex) {
                    Logger.getLogger(RFID.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        // Das RFIDPhidget wird abgehängt
        rfid.addDetachListener(new DetachListener(){

            @Override
            public void detached(DetachEvent de) {
                RFIDPhidget rfid = (RFIDPhidget) de.getSource();
                try {
                    System.out.println("RFID.java: RFID Disconnected, \tID:"+rfid.getDeviceID()+"\tNAME:"+rfid.getDeviceName());
                } catch (PhidgetException ex) {
                    Logger.getLogger(RFID.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
        });
        // Ein wilder Tag erscheint!
        rfid.addTagGainListener(new TagGainListener(){

            @Override
            public void tagGained(TagGainEvent tge) {
                RFIDPhidget rfid = (RFIDPhidget) tge.getSource();
                setTag(tge.getValue());
                System.out.println("RFID.java: Set Tag to "+tge.getValue());
                RFID.ps.setText(tge.getValue());
                try {
                    rfid.setLEDOn(true);
                } catch (PhidgetException ex) {
                    Logger.getLogger(RFID.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
        });
        // Der wilde Tag ist geflohen.
        rfid.addTagLossListener(new TagLossListener(){

            @Override
            public void tagLost(TagLossEvent tle) {
                RFIDPhidget rfid = (RFIDPhidget) tle.getSource();
                setTag(null);
                System.out.println("RFID.java: Set Tag to null");
                RFID.ps.setText("");
                try {
                    rfid.setLEDOn(false);
                } catch (PhidgetException ex) {
                    Logger.getLogger(RFID.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
        });
        
        Thread th_1 = new Thread(){
            @Override
            public void run(){
                try {
                    rfid.openAny();
                    rfid.waitForAttachment();
                    rfid.setAntennaOn(true);
                } catch (PhidgetException ex) {
                    Logger.getLogger(RFID.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };
        th_1.start();
       
  
        
    }
    public static void setPenis(JTextField p){
        RFID.ps=p;
    }
    public static RFID getInstance(JTextField p){
        if(rfid == null){
            try {
                rfid = new RFID(p);
                return rfid;
            } catch (PhidgetException ex) {
                return rfid;
            }
        }else{
            setPenis(p);
            return rfid;
        }
    }
    
    public String getTag(){
        return this.Tag;
    }
    
    private void setTag(String tag){
        this.Tag = tag;
    }
    
    public static void waitms(int n) {
        long t0, t1;
        t0 = System.currentTimeMillis();
        do {
            t1 = System.currentTimeMillis();
        } while (t1 - t0 < n);
    }
    
    
}
